import { generate } from '@/api/live/live';
import { ScriptType } from '@/enum/enums';
import { EventBus } from '@/event-bus.js';
export const keyWordMatch = {
  data() {
    return {

    };
  },
  created() {
    EventBus.$on("keywordMatchSendWebSocket", () => {
      if (this.keywordSetting.open) {
        this.matchKeyWord();
      }
    });
  },
  beforeDestroy() {
    EventBus.$off('keywordMatchSendWebSocket');
  },
  mounted() { },
  computed: {
    pageConfig() {
      return this.$store.state.page.config;
    },
    keywordSetting() {
      return this.pageConfig.keywordSetting;
    },
    keyWordContentList() {
      let list = [];
      const content = this.$store.state.page.keyWordContent;
      if (!content) {
        return list;
      }
      content.split('\n').filter(line => line.trim() !== '').map(line => {
        const lineItems = line.split('-----');
        if (lineItems.length >= 2) {
          list.push({
            keyWords: lineItems[0].split('|'),
            contents: lineItems[1].split('\\')
          });
        }
      });
      return list;
    },
  },
  methods: {
    matchKeyWord() {
      const now = Date.now();
      const timeThreshold = now - 50000;
      // 筛选出过去50秒内的新弹幕
      const userBarrageList = this.userBarrageDataList.filter(user => user.timestamp >= timeThreshold && !user.hasGen);
      if (userBarrageList.length > 0) {
        let findMatchItemResult = null;
        userBarrageList.find(userBarrage => {
          const findMatchItem = this.keyWordContentList.find(item => {
            return item.keyWords.some(keyWord => {
              //如果包含了这个关键字 并且不能是屏蔽字
              if (this.keywordSetting.blockWords) {
                let blockWordList = this.keywordSetting.blockWords.split('\n');
                if (blockWordList.length > 0) {
                  let findBlockWordItem = blockWordList
                    .filter(blockWord => blockWord.trim() !== '')
                    .find(blockWord => userBarrage.content.includes(blockWord));
                  if (findBlockWordItem) {
                    return false;
                  }
                }
              }
              return userBarrage.content.includes(keyWord);
            });
          });
          if (findMatchItem) {
            findMatchItemResult = {
              ...findMatchItem,
              userBarrage: userBarrage
            }
            return true;
          }
        });
        if (findMatchItemResult) {
          const text = findMatchItemResult.contents[Math.floor(Math.random() * findMatchItemResult.contents.length)];
          let nickName = findMatchItemResult.userBarrage.nickName;
          if (this.pageConfig.nameSize != '') {
            nickName = nickName.substring(0, this.pageConfig.nameSize);
          }
          //匹配到关键字 就要将这个人去除掉
          let findUserBarrage = this.userBarrageDataList.find(item => item.id == findMatchItemResult.userBarrage.id);
          if (findUserBarrage) {
            findUserBarrage.hasGen = true;
            findUserBarrage.hasReply = true;
          }
          generate({
            userName: nickName,
            text: text,
            problemText: findMatchItemResult.userBarrage.content,
            scriptType: ScriptType.KEYWORD.code,
          });
        }
      }
    }
  }
}
